#include<iostream>
#include<sys/wait.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<vector>
using namespace std;

void Worker(int number)
{
    int cnt = 2;
    while(cnt)
    {
        cout << "I am a child process, pid:" << getpid() <<" ppid:" << getppid() << " num:" << number <<endl;
        sleep(1);
        cnt--;
    }
    //int a = 10 / 0;
}
const int n = 10;
int main()
{
    for(int i = 0; i < n; i++)
    {
        pid_t id = fork();
        if(0 == id)
        {
            Worker(i);
            exit(i);
        }
    }
    for(int i = 0; i < n; i++)
    {
        int status = 0;
        pid_t rid = waitpid(-1, &status, 0);
        if(rid > 0)
        {
            cout << "wait child " << rid << " success" << " rid:" <<WEXITSTATUS(status) << endl;
        }
    }

    //pid_t id = fork();
    //if(id == 0)
    //{
    //    Worker();
    //    exit(10);
    //}
    //else
    //{
    //    int status = 0;
    //    //sleep(10);
    //    cout << "wait before" << endl;
    //    pid_t rid = waitpid(id, &status, 0);
    //    cout << "wait after" << endl;
    //    if(rid == id)
    //    {
    //        //cout << "wait success, pid:" << getpid() << " ppid:" << getppid() << endl;
    //        //cout << "rid:" << rid << endl;
    //        //cout << "exit sig:" << (status&0x7F) << endl;
    //        //cout << "exit code:" << (status>>8) << endl;
    //        if(WIFEXITED(status))
    //        {
    //            cout << "child process normal quit, exit code:" << WEXITSTATUS(status) << endl;
    //        }
    //        else
    //        {
    //            cout << "child process quit except:" << WEXITSTATUS(status) << endl;
    //        }
    //    }
    //    sleep(10);
    //}
    return 0;
}

//int func()
//{
//    cout << "call func function done" << endl;
//    _exit(22);
//}
//int main()
//{
//    cout << "you can see me";
//    sleep(3);
//    _exit(1);
//
//    //func();
//    //cout << "I am a normal process, pid:" << getpid() <<",ppid:" <<getppid() << endl;
//    //_exit(11);
//    //return 0;
//}
